package com.joysuch.wwyt.workflow.service.impl;

import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.workflow.bean.WorkFlowBizTypeBean;
import com.joysuch.wwyt.workflow.bean.WorkFlowBizTypeTreeBean;
import com.joysuch.wwyt.workflow.bean.WorkFlowFormTemplateTypeTreeBean;
import com.joysuch.wwyt.workflow.entity.WorkFlowBizType;
import com.joysuch.wwyt.workflow.entity.WorkFlowFormTemplateType;
import com.joysuch.wwyt.workflow.repository.WorkFlowBizTypeDao;
import com.joysuch.wwyt.workflow.service.WorkFlowBizTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class WorkFlowBizTypeServiceImpl implements WorkFlowBizTypeService {

    @Autowired
    private WorkFlowBizTypeDao bizTypeDao;


    @Override
    public ResultBean save(WorkFlowBizTypeBean bean) {
        WorkFlowBizType bizType = null;
        if(bean.getId() == null || bean.getId() ==0){
            bizType = new WorkFlowBizType();
            bizType.setState(1);
            if(checkDuplicateName(bean, true)){
              return ResultBean.wrapErrorResultDefault("类型名称已经存在，不能重复");
            }
        }else{
            bizType = findById(bean.getId());
            if(bizType == null){
                return ResultBean.wrapErrorResultDefault("当前的表单模板已不存在，无法保存。");
            }
            if(checkDuplicateName(bean, false)){
                return ResultBean.wrapErrorResultDefault("类型名称已经存在，不能重复");
            }
        }
        BeanUtils.copyProperties(bean, bizType);
        bizTypeDao.save(bizType);
        return ResultBean.success(bizType);
    }

    private boolean checkDuplicateName(WorkFlowBizTypeBean bean, boolean isNew){
        boolean result  = false;
        WorkFlowBizType bizType = bizTypeDao.findFirstByNameAndParentIdAndProcessId(bean.getName(),bean.getParentId(), bean.getProcessId());
        if(isNew){
           if(bizType != null){
               return true;
           }
        }else{
            if(bizType != null && bean.getId()!= bizType.getId()){
                return true;
            }
        }
        return result;
    }

    @Override
    public ResultBean delete(Long id) {
//        Integer count  = bizTypeDao.findCountByType(id);
//        if(count != null && count >0 ){
//            return ResultBean.wrapErrorResultDefault("当前类型尚有绑定的表单模板，请先解除绑定关系后再删除。");
//        }
        bizTypeDao.deleteById(id);
        return ResultBean.defaultSuccessResult();
    }

    @Override
    public WorkFlowBizType findById(Long id) {
        WorkFlowBizType formTemplateType =  bizTypeDao.findById(id).orElse(null);
        if(formTemplateType!= null && formTemplateType.getParentId() != null){
            WorkFlowBizType parent = bizTypeDao.findById(formTemplateType.getParentId()).orElse(null);
            if(parent!= null){
                formTemplateType.setParentName(parent.getName());
            }
        }
        return formTemplateType;
    }

    @Override
    public List<WorkFlowBizType> findAll(Long processId){
        return bizTypeDao.findAllByState(processId);
    }


    @Override
    public List<WorkFlowBizTypeTreeBean> listTree(Long processId){
        List<WorkFlowBizTypeTreeBean> list = new ArrayList<>();
        List<WorkFlowBizType> all = findAll(processId);
        if(all != null && !all.isEmpty()){
            Map<Long, WorkFlowBizTypeTreeBean> treeMap = new HashMap<>();
            for(WorkFlowBizType type: all) {
                WorkFlowBizTypeTreeBean bean = new WorkFlowBizTypeTreeBean();
                bean.setId(type.getId());
                bean.setLabel(type.getName());
                bean.setChildren(new ArrayList<>());
                treeMap.put(type.getId(), bean);
                if(type.getParentId() == null || type.getParentId()==0){
                    list.add(bean);
                }else{
                    WorkFlowBizTypeTreeBean parent= treeMap.get(type.getParentId());
                    parent.getChildren().add(bean);
                    treeMap.put(parent.getId(), parent);
                }
            }

        }
        return  list;
    }
}
